package vashion.azeroth.core.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import vashion.azeroth.contant.AzerothConstants;
import vashion.azeroth.contant.GenericErrorEnum;
import vashion.azeroth.contant.InviteErrorEnum;
import vashion.azeroth.core.manager.personal.PersonalInviteCodeManager;
import vashion.azeroth.core.manager.personal.PersonalMemberManager;
import vashion.azeroth.core.personal.pojo.PersonalInviteCode;
import vashion.azeroth.core.personal.pojo.PersonalMember;
import vashion.azeroth.core.result.ApiResult;
import vashion.azeroth.core.service.PersonalInviteCodeService;

@Service
public class PersonalInviteCodeServiceImpl implements PersonalInviteCodeService<PersonalInviteCode> {

	@Autowired
	private PersonalInviteCodeManager personalInviteCodeManager;
	
	@Autowired
	private PersonalMemberManager personalMemberManager;
	
	@Override
	public ApiResult<PersonalInviteCode> generateInviteCode(String memberId, long operatorId) {
		if(StringUtils.isBlank(memberId)) {
			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EMPTY.getErrorCode(), "memberId" + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
		}
		
		PersonalMember member = personalMemberManager.get(Long.valueOf(memberId));
		
		if(null == member) {
			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EXIST.getErrorCode(), "用户" + GenericErrorEnum.NOT_EXIST.getErrorMessage());
		}
		
		operatorId = Long.valueOf(memberId);
		
		Integer count = personalInviteCodeManager.generateInviteCode(member, operatorId);
		
		if(count > 0)
			return ApiResult.newSuccessResult();
		
		return ApiResult.newErrorResult(InviteErrorEnum.FAILED.getErrorCode(), InviteErrorEnum.FAILED.getErrorMessage());
	}

	@Override
	public ApiResult<PersonalInviteCode> getInviteCodeByCode(String code) {
		
		if(null == code) {
			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EMPTY.getErrorCode(), "邀请码" + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
		}
		
		PersonalInviteCode inviteCodeParam = new PersonalInviteCode();
		inviteCodeParam.setCode(code);
		//inviteCodeParam.setStatus(AzerothConstants.PersonalInviteCode.PersonalInviteCodeStatus.unused);
		
		PersonalInviteCode inviteCode = personalInviteCodeManager.getInviteCodeByCode(inviteCodeParam);
		
		if(null == inviteCode) {
			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EXIST.getErrorCode(), "邀请码" + GenericErrorEnum.NOT_EXIST.getErrorMessage());
		}
		
		return ApiResult.newSuccessResult(inviteCode);
	}

	@Override
	public ApiResult<List<PersonalInviteCode>> getAllInviteCode(PersonalInviteCode inviteCodeParam) {
		
		if (null != inviteCodeParam.getMemberId()) {
			PersonalMember member = personalMemberManager.get(inviteCodeParam.getMemberId());
			
			if(null == member) {
				return ApiResult.newErrorResult(GenericErrorEnum.NOT_EXIST.getErrorCode(), "用户" + GenericErrorEnum.NOT_EXIST.getErrorMessage());
			}
		}
		
		List<PersonalInviteCode> list = personalInviteCodeManager.findAll(inviteCodeParam);
		
		return ApiResult.newSuccessResult(list);
	}

	@Override
	public ApiResult<Boolean> freezeInviteCode(long inviteCodeId, long operatorId) {
		if (inviteCodeId <= 0 || operatorId <= 0) {
			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EXIST.getErrorCode(), "邀请码" + GenericErrorEnum.NOT_EXIST.getErrorMessage());
		}
		//查询是否存在
		PersonalInviteCode inviteCode = personalInviteCodeManager.get(inviteCodeId);
		if (null == inviteCode) {
			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EXIST.getErrorCode(), "邀请码" + GenericErrorEnum.NOT_EXIST.getErrorMessage());
		}
		//如果已经被用，不能被修改
		if (AzerothConstants.PersonalInviteCode.PersonalInviteCodeStatus.used == inviteCode.getStatus()) {
			return ApiResult.newErrorResult(InviteErrorEnum.USED.getErrorCode(), InviteErrorEnum.USED.getErrorMessage());
		}
		inviteCode.setStatus(AzerothConstants.PersonalInviteCode.PersonalInviteCodeStatus.disabled);
		//修改状态
		personalInviteCodeManager.update(inviteCode);
		return ApiResult.newSuccessResult(true);
	}

	@Override
	public ApiResult<Boolean> resumeInviteCode(long inviteCodeId, long operatorId) {
		if (inviteCodeId <= 0 || operatorId <= 0) {
			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EXIST.getErrorCode(), "邀请码" + GenericErrorEnum.NOT_EXIST.getErrorMessage());
		}
		//查询是否存在
		PersonalInviteCode inviteCode = personalInviteCodeManager.get(inviteCodeId);
		if (null == inviteCode) {
			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EXIST.getErrorCode(), "邀请码" + GenericErrorEnum.NOT_EXIST.getErrorMessage());
		}
		//如果已经被用，不能被修改
		if (AzerothConstants.PersonalInviteCode.PersonalInviteCodeStatus.used == inviteCode.getStatus()) {
			return ApiResult.newErrorResult(InviteErrorEnum.USED.getErrorCode(), InviteErrorEnum.USED.getErrorMessage());
		}
		inviteCode.setStatus(AzerothConstants.PersonalInviteCode.PersonalInviteCodeStatus.unused);
		//修改状态
		personalInviteCodeManager.update(inviteCode);
		return ApiResult.newSuccessResult(true);
	}

	
}
